/**
 * fileName:  RegionDictReader.java
 * author:  lr
 * Company:  Cloudland Studio
 * Createdate:  2014-7-8 下午04:48:42
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-7-8          lr            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.store.read;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.cloudland.commercial.masterdata.store.DatabaseReader;
import org.cloudland.commercial.masterdata.store.entity.CategoryEntity;
import org.cloudland.commercial.masterdata.store.entity.RegionEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.FruitCategoryEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.RegionDictEntity;
import org.cloudland.jdbc.ibatis.masterdata.handler.RegionDictMapper;
import org.springframework.stereotype.Service;

 /**
 * <pre> 
 * 地区字典管理读操作
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  RegionDictReader
 * @author  lr
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
@Service
public class RegionDictReader implements DatabaseReader<RegionEntity> {
	@Resource(name = "regionDictMapper")
	private RegionDictMapper regionDictMapper;
	
	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public RegionDictReader() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * <pre>
	 * 查询根节点
	 * </pre>
	 *
	 * @return   
	 */
	public List<RegionEntity> findRegionRoot() {
		return findChildRegionById("");
	}
	
	/**
	 * <pre>
	 * 根据编号查询
	 * </pre>
	 *
	 * @param categoryId
	 * @return   
	 */
	public RegionEntity findById(String regionId) {
		RegionDictEntity daoEntity = regionDictMapper.findById(regionId);
		
		RegionEntity entity = null;
		if (null != daoEntity) {
			entity = new RegionEntity(daoEntity.getRegionId(), daoEntity.getRegionName(), daoEntity.getSort(), daoEntity.getRegionParentId());
		}
		return entity;		
	}
	
	/**
	 * <pre>
	 * 根据编号查询子节点
	 * </pre>
	 *
	 * @param regionId
	 * @return   
	 */
	public List<RegionEntity> findChildRegionById(String regionId) {
		
		List<RegionDictEntity> arrayChild = regionDictMapper.findChildById(regionId);
		if (null == arrayChild || arrayChild.size() < 1) {
			return null;
		}
		
		List<RegionEntity> resultArray = new ArrayList<RegionEntity>(arrayChild.size());
		for (RegionDictEntity daoEntity : arrayChild) {
			resultArray.add(new RegionEntity(daoEntity.getRegionId(), daoEntity.getRegionName(), daoEntity.getSort(), daoEntity.getRegionParentId()));
		}
		
		return resultArray;		
	}
}
